'use strict';
$(function () {
    var calInstance = $('#calendar');
    let isFullRoom  = false;
    let checkedDate = '';
    calInstance.fullCalendar('removeEvents');

    function renderCal() {
        calInstance.fullCalendar({
            defaultDate     : disableDate,
            eventColor      : '#3BB2E3',
            monthNames      : ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
            validRange      : {start: disableDate, end: endDate},
            buttonText      : {
                today: '今天',
                month: '月',
                week : '周',
                day  : '日',
                prev : '上一月',
                next : '下一月'
            },
            dayNames        : ['日', '一', '二', '三', '四', '五', '六'],
            dayNamesShort   : ['日', '一', '二', '三', '四', '五', '六'],
            header          : {
                left  : 'prev,next today',
                center: 'title',
                right : 'month,agendaWeek,agendaDay,listWeek'
            },
            eventClick      : function (calEvent, jsEvent, view) {
                let index = $(this).parent().index();
                $(this).parents('.fc-day-grid.fc-unselectable').find('tr td').css('background-color', 'transparent');
                $(this).parents('.fc-content-skeleton').prev().find(`tr td:eq(${index})`).css('background-color', '#fff9db');
                $('.index-gallery button').addClass('layui-btn-disabled');
            },
            dayClick        : function (date, allDay, jsEvent, view) {
                $(this).parents('.fc-body').find('.fc-day').css('background-color', 'transparent');
                $(this).css('background-color', '#fff9db');

                var events = calInstance.fullCalendar('clientEvents', function (event) {
                    var eventStart = event.start.format('YYYY-MM-DD');
                    var eventEnd   = event.end ? event.end.format('YYYY-MM-DD') : null;
                    var theDate    = date.format('YYYY-MM-DD');
                    return (eventStart <= theDate && (eventEnd >= theDate) && !(eventStart < theDate && (eventEnd === theDate))) || (eventStart === theDate && (eventEnd === null));
                });
                if (events.length) {
                    $('.index-gallery button').addClass('layui-btn-disabled');
                    return;
                }
                checkedDate = $.fullCalendar.formatDate(date, 'Y-MM-DD');
                $('.index-gallery button').removeClass('layui-btn-disabled')
            },
            dayRender       : function (date, cell) {
                let cur   = $.fullCalendar.formatDate(date, 'Y-MM-DD');
                var today = new Date();
                var y     = today.getFullYear();
                var m     = today.getMonth() + 1;
                m         = m < 10 ? '0' + m : m;
                var d     = today.getDate();
                d         = d < 10 ? ('0' + d) : d;
                today     = y + '-' + m + '-' + d;
                if (cur === today) {
                    calInstance.fullCalendar('renderEvent', {
                        title    : '今天',
                        allday   : true,
                        start    : today,
                        color    : '#ffcaca3b',
                        textColor: 'red'
                    }, false)
                }
                if (isFullRoom) {
                    if (cur !== today) {
                        calInstance.fullCalendar('renderEvent', {
                            title    : '满',
                            allday   : true,
                            start    : cur,
                            color    : '#ffcaca3b',
                            textColor: 'red'
                        }, false)
                    }
                }

            },
            viewRender      : function () {
                $('.fc-row.fc-week.fc-widget-content').find('.fc-content-skeleton tbody').each(function () {
                    if ($(this).find('tr').length > 1) {
                        $(this).find('tr').eq(0).siblings().remove();
                    }
                });
            }
        });
    }

    /*$(document).on('click', '.fc-day', function () {
        $(this).parents('.fc-body').find('.fc-day').css('background-color', 'transparent');
        $(this).css('background-color', '#fff9db');
        $('.index-gallery button').removeClass('layui-btn-disabled')
    });*/

    $.get('/api/index/getDateMap', function (res) {
        if (res.code === 10001) {
            isFullRoom = true;
        } else {
            if (res.code !== 1) {
                return layer.msg('您当前网络有延迟，请刷新重试');
            }
        }
        renderCal();

        if (res.data.length) {
            $.each(res.data, function (index, term) {
                calInstance.fullCalendar('renderEvent', {
                    title    : '满',
                    start    : term,
                    color    : '#ffcaca3b',
                    textColor: 'red'
                }, true);
            });
        }
    }).fail(function () {
        return layer.msg('您当前网络有延迟，请刷新重试');
    });

    $('.index-gallery').click(function () {
        if ($(this).find('button').hasClass('layui-btn-disabled')) {
            return;
        }
        location.href = 'order?date=' + checkedDate;
    });
});

